package com.hs.car.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.hs.car.pojo.Car;
import com.hs.car.service.ICarService;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xudongzhou
 * @date 2022-07-21
 * @desc
 */
@RestController
@RequestMapping("/car")
@Slf4j
@Api(tags = "车辆管理模块")
public class TestController {

//    @Autowired
//    private IOrdersService ordersService;
//
//    @GetMapping("/orders")
//    @ApiOperation("通过车牌号查询车辆下所有订单信息")
//    public ResultVo findOrders(Integer currentPage,Integer pageSize,String carNumber,String status){
//
//        IPage<Orders> page = new Page<>(currentPage,pageSize);
//
//        QueryWrapper qr = new QueryWrapper<>();
//
//
//        qr.eq("car_license_plate",carNumber);
//
//        Integer integerStatus = null;
//        if(!StringUtils.isEmpty(status)){
//            if(status.equals("未支付")){ integerStatus = 0;}
//            else if(status.equals("已支付")){ integerStatus = 1;}
//            else if(status.equals("异常订单")){ integerStatus = 2;}
//            else { integerStatus = 3;}
//
//            qr.eq("status",integerStatus);
//        }
//
//
//        IPage orderPage = ordersService.page(page, qr);
//
//        List records = orderPage.getRecords();
//        Long total = orderPage.getTotal();
//
//        if(!CollectionUtils.isEmpty(records)){
//            ResultVo resultVo = new ResultVo(ResultCode.SUCCESS);
//            PageBean<Orders> pageBean = new PageBean<>();
//            pageBean.setList(records);
//            pageBean.setTotals(total.intValue());
//
//            resultVo.setData(pageBean);
//            resultVo.setMsg("查询成功");
//
//            return resultVo;
//        }
//        return new ResultVo(ResultCode.FAILED);
//    }


    @Autowired
    private ICarService carService;

    @GetMapping("/downloadExcel")
    @ApiOperation("导出车辆信息")
    @ResponseBody
    public ResultVo downLoadFile(HttpServletResponse response) throws ClassNotFoundException, IOException {

        ExcelWriter writer = ExcelUtil.getWriter();
        List<Car> cars = carService.list();

        List<Map<String,Object>> rows = cars.stream().map(item -> {
            Map<String,Object> maps = new HashMap<>();
            maps.put("licensePlate",item.getLicensePlate());
            maps.put("telephone",item.getTelephone());
            maps.put("plateType",item.getPlateType());
            maps.put("carStatus",item.getCarStatus());
            return maps;
        }).collect(Collectors.toList());

        //Title
        int columns = Class.forName("com.hs.car.pojo.Car").getDeclaredFields().length;

        writer.merge(columns - 1,"车辆信息");

        //Header
        writer.addHeaderAlias("licensePlate","车牌号");
        writer.addHeaderAlias("telephone","电话");
        writer.addHeaderAlias("plateType","类型");
        writer.addHeaderAlias("carStatus","车辆状态");

        //Body
        writer.setColumnWidth(0,100);
        writer.setColumnWidth(1,100);
        writer.setColumnWidth(2,100);
        writer.setColumnWidth(3,100);

        writer.write(rows, true);

        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("车辆信息表-" + DateUtil.today() + ".xls", "utf-8"));


        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);

        writer.close();
        IoUtil.close(out);
        return new ResultVo(ResultCode.SUCCESS);

    }
}
